package com.ty.zls.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.ty.zls.R;
import com.ty.zls.adapter.PreSortAdapter;
import com.ty.zls.bean.Account;
import com.ty.zls.components.MyReceycleView;
import com.ty.zls.listener.NeedWaterItemListener;
import com.ty.zls.task.PreSortTask;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PresortActivity extends AppCompatActivity {

    @ViewInject(R.id.presort_topbar)
    private QMUITopBarLayout presort_topbar;
    @ViewInject(R.id.swipeToLoadLayout)
    private SwipeToLoadLayout swipeToLoadLayout;
    @ViewInject(R.id.swipe_target)
    private MyReceycleView receycleView;
    @ViewInject(R.id.presort_empty)
    private QMUIEmptyView presort_empty;
    private Map<String,String> request = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        QMUIStatusBarHelper.translucent(this);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_presort);
        x.view().inject(this);

        initTopBar();
        initPager();
    }

    private void initPager() {
        receycleView.setEmptyView(presort_empty);
        receycleView.setLayout(swipeToLoadLayout);

        final List<Account> accounts = new ArrayList<>();
        final PreSortAdapter adapter = new PreSortAdapter(accounts, this);
        receycleView.setLayoutManager(new LinearLayoutManager(this));
        receycleView.setAdapter(adapter);
        OnRefreshListener onRefreshListener = new OnRefreshListener() {
            @Override
            public void onRefresh() {
                swipeToLoadLayout.setRefreshing(false);
                adapter.setPageNum(1);
                adapter.setTotal(0);
                adapter.getDatas().clear();
                request.put("pageNum", String.valueOf(1));
                request.put("pageSize", String.valueOf(adapter.getPageSize()));
                PreSortTask task = new PreSortTask(presort_empty,PresortActivity.this,adapter,swipeToLoadLayout);
                task.execute(request);
            }
        };
        swipeToLoadLayout.setOnRefreshListener(onRefreshListener);
        swipeToLoadLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                request.put("pageNum", String.valueOf(adapter.getPageNum()));
                request.put("pageSize", String.valueOf(adapter.getPageSize()));
                PreSortTask task = new PreSortTask(presort_empty,PresortActivity.this,adapter,swipeToLoadLayout);
                task.execute(request);
                swipeToLoadLayout.setLoadingMore(false);
            }
        });
        adapter.setOnItemClickListener(new NeedWaterItemListener(this, adapter));
        autoRefresh();
    }

    private void initTopBar() {
        presort_topbar.setTitle("置后用户");
        presort_topbar.setBackgroundColor(getResources().getColor(R.color.app_color_theme_7));
        presort_topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PresortActivity.this.finish();
            }
        });
    }

    public void autoRefresh() {
        swipeToLoadLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeToLoadLayout.setRefreshing(true);
            }
        });
    }

}
